package main.queue;

import java.lang.reflect.Array;

/**
 * 循环队列
 * @author: kejl
 * @create: 2020-07-06 21:24
 */
public class CircularQueue {

    private Integer[] queueArray = null;

    private int head = 0;

    private int tail = 0;


    /** Initialize your data structure here. Set the size of the queue to be k. */
    public CircularQueue(int k) {
        queueArray = new Integer[k];

    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }else{
            //在最后一个数组元素上，指针变为0，需要添加到第一个去,其他+1 指针+1
            if(tail==queueArray.length-1){
                tail = 0;
                queueArray[queueArray.length-1] = value;
            }else {
                queueArray[tail] = value;
                tail = tail+1;
            }
            return true;
        }

    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if(isEmpty()){
            return  false;
        }else{
            //下个元素在队尾，指针在最后了，如果不是指针+1
            queueArray[head] = null;
            if(head==queueArray.length-1){
                head = 0;
            }else{
                head = head+1;
            }
        return true;
        }

    }

    /** Get the front item from the queue. */
    public int Front() {
        if (!isEmpty()){
            return queueArray[head];
        }else{
            return 0;
        }

    }

    /** Get the last item from the queue. */
    public int Rear() {
        if (!isEmpty()){
            return queueArray[tail];
        }else{
            return 0;
        }
    }

    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
            //只需要看上一个
        if(queueArray[head]==null){
            return true;
        }else{
            return  false;
        }
    }

    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {

        if(queueArray[tail]==null){
            return false;
        }else{
            return  true;
        }
    }

}
